<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: IndexController.php 9339 2011-09-29 23:03:01Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class User_IndexController extends Core_Controller_Action_Standard {
	public function indexAction() {
	
	}
	
	public function homeAction() {
		// check public settings
		$require_check = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.general.portal', 1 );
		if (! $require_check) {
			if (! $this->_helper->requireUser ()->isValid ())
				return;
		}
		
		if (! Engine_Api::_ ()->user ()->getViewer ()->getIdentity ()) {
			return $this->_helper->redirector->gotoRoute ( array (), 'default', true );
		}
		
		// Render
		$this->_helper->content->setNoRender ()->setEnabled ();
	}
	
	public function browseAction() {
		$require_check = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.general.browse', 1 );
		if (! $require_check) {
			if (! $this->_helper->requireUser ()->isValid ())
				return;
		}
		if (! $this->_executeSearch ()) {
			throw new Exception ( 'error' );
		}
		
		if ($this->_getParam ( 'ajax' )) {
			$this->renderScript ( '_browseUsers.tpl' );
		}
	}
	
	protected function _executeSearch() {
		// Check form
		$form = new User_Form_Search ( array ('type' => 'user' ) );
		
		if (! $form->isValid ( $this->_getAllParams () )) {
			$this->view->error = true;
			return false;
		}
		
		$this->view->form = $form;
		
		// Get search params
		$page = ( int ) $this->_getParam ( 'page', 1 );
		$ajax = ( bool ) $this->_getParam ( 'ajax', false );
		$options = $form->getValues ();
		
		// Process options
		$tmp = array ();
		foreach ( $options as $k => $v ) {
			if (null == $v || '' == $v || (is_array ( $v ) && count ( array_filter ( $v ) ) == 0)) {
				continue;
			} else if (false !== strpos ( $k, '_field_' )) {
				list ( $null, $field ) = explode ( '_field_', $k );
				$tmp ['field_' . $field] = $v;
			} else if (false !== strpos ( $k, '_alias_' )) {
				list ( $null, $alias ) = explode ( '_alias_', $k );
				$tmp [$alias] = $v;
			} else {
				$tmp [$k] = $v;
			}
		}
		$options = $tmp;
		
		// Get table info
		$table = Engine_Api::_ ()->getItemTable ( 'user' );
		$userTableName = $table->info ( 'name' );
		
		$searchTable = Engine_Api::_ ()->fields ()->getTable ( 'user', 'search' );
		$searchTableName = $searchTable->info ( 'name' );
		
		//extract($options); // displayname
		$profile_type = @$options ['profile_type'];
		$displayname = @$options ['displayname'];
		extract ( $options ['extra'] ); // is_online, has_photo, submit
		

		// Contruct query
		$select = $table->select ()->//->setIntegrityCheck(false)
		from ( $userTableName )->joinLeft ( $searchTableName, "`{$searchTableName}`.`item_id` = `{$userTableName}`.`user_id`", null )->//->group("{$userTableName}.user_id")
		where ( "{$userTableName}.search = ?", 1 )->where ( "{$userTableName}.enabled = ?", 1 )->order ( "{$userTableName}.displayname ASC" );
		
		// Build the photo and is online part of query
		if (! empty ( $has_photo )) {
			$select->where ( $userTableName . '.photo_id != ?', "0" );
		}
		
		if (! empty ( $is_online )) {
			$select->joinRight ( "engine4_user_online", "engine4_user_online.user_id = `{$userTableName}`.user_id", null )->group ( "engine4_user_online.user_id" )->where ( $userTableName . '.user_id != ?', "0" );
		}
		
		// Add displayname
		if (! empty ( $displayname )) {
			$select->where ( "(`{$userTableName}`.`username` LIKE ? || `{$userTableName}`.`displayname` LIKE ?)", "%{$displayname}%" );
		}
		
		// Build search part of query
		$searchParts = Engine_Api::_ ()->fields ()->getSearchQuery ( 'user', $options );
		foreach ( $searchParts as $k => $v ) {
			$select->where ( "`{$searchTableName}`.{$k}", $v );
		}
		
		// Build paginator
		$paginator = Zend_Paginator::factory ( $select );
		$paginator->setItemCountPerPage ( 10 );
		$paginator->setCurrentPageNumber ( $page );
		
		$this->view->page = $page;
		$this->view->ajax = $ajax;
		$this->view->users = $paginator;
		$this->view->totalUsers = $paginator->getTotalItemCount ();
		$this->view->userCount = $paginator->getCurrentItemCount ();
		$this->view->topLevelId = $form->getTopLevelId ();
		$this->view->topLevelValue = $form->getTopLevelValue ();
		$this->view->formValues = array_filter ( $options );
		
		return true;
	}
}